const STATUS = {
  OFFLINE: 0,
  ONLINE: 1,
  DELETED: 2
};
const PAY_STATUS = {
  NO_PAYED: 0,
  PAYED: 1
};
const AUTH_STEP = {
  WAITED: 0, // 待审核，
  PASSED: 1, // 审核通过
  NO_PASSED: 2 // 审核不通过
};
module.exports = class extends think.Model {
  incViewCount(id) {
    return this.where({
      id
    }).increment('viewed_count', 1);
  }
  upCount(id) {
    return this.where({
      id
    }).increment('up_count', 1);
  }
  async queryUserIdById(id) {
    return this.where({
      id
    }).getField('user_id', true);
  }
  async queryMaxId({
    userId = 0,
    categoryId = 0,
    keywords = ''
  }) {
    const where = {
      status: STATUS.ONLINE,
      auth_step: AUTH_STEP.PASSED,
      is_payed: PAY_STATUS.PAYED
    };
    if (categoryId > 0) {
      where.category_id = categoryId;
    }
    if (userId > 0) {
      where.user_id = userId;
    }
    if (keywords) {
      // 以后优化
      where.content = ['like', `%${keywords}%`];
    }
    return this.where(where).max('id');
  }
  // 查询信息列表
  async queryOnlineData({
    page = 1,
    pageSize = 10,
    userId = 0,
    categoryId = 0,
    lat = 0,
    lastId = 0,
    lng = 0,
    keywords = '',
    orderType = 0
  }) {
    const where = {
      status: STATUS.ONLINE,
      auth_step: AUTH_STEP.PASSED,
      is_payed: PAY_STATUS.PAYED
    };
    if (categoryId > 0) {
      where.category_id = categoryId;
    }
    if (userId > 0) {
      where.user_id = userId;
    }
    if (keywords) {
      // 以后优化
      where.content = ['like', `%${keywords}%`];
    }
    if (lastId > 0) {
      where.id = ['<=', lastId];
    }
    if (orderType > 0) {
      // order('is_top desc')
      // 按距离排序
      this.order('s_distance(lat,lng,' + lat + ',' + lng + ') asc,is_top desc,id desc');
    } else {
      this.order('is_top desc,id desc');
    }
    const data = await this.where(where)
      .alias('a')
      .field([
        'id', 'user_id', 'content', 'img_srcs', 'address', 'telephone',
        'create_time', 'viewed_count', 'up_count', 'category_id', 'comment_count',
        '(select title from category where id=a.category_id) category_name',
        '(select nickname from user where id=a.user_id) nickname',
        '(select avatar from user where id=a.user_id) avatar'
      ])
      .page(page, pageSize).where(where).select();
    return data;
  }
  // 发布信息
  create({
    content,
    imgSrcs,
    lat,
    lng,
    userId,
    isPayed,
    status,
    address,
    telephone,
    totalAmount,
    topAmount,
    topDays = 0,
    isTop = 0,
    categoryId
  }) {
    const data = {};
    data.content = content;
    data.img_srcs = imgSrcs;
    data.lng = lng;
    data.lat = lat;
    data.user_id = userId;
    data.address = address;
    data.is_payed = isPayed;
    data.status = status;
    data.telephone = telephone;
    data.create_time = think.datetime();
    data.top_days = topDays;
    data.is_top = isTop;
    data.total_amount = totalAmount;
    data.top_amount = topAmount;
    // 置顶
    if (isTop) {
      data.top_time = data.create_time;
    }
    data.category_id = categoryId;
    // 创建新的记录
    return this.add(data);
  }
  // 查询某条记录
  queryById(id) {
    return this.alias('a').where({
      id
    }).field([
      'id', 'user_id', 'content', 'img_srcs', 'address', 'telephone',
      'create_time', 'viewed_count', 'up_count', 'comment_count',
      '(select title from category where id=a.category_id) category_name'
    ]).find();
  }
  // 修改信息
  updateById(id, {
    content,
    imgSrcs,
    address,
    lat,
    lng,
    telephone
  }) {
    return this.where({
      id
    }).update({
      content,
      modify_time: think.datetime(),
      img_srcs: imgSrcs,
      address,
      lat,
      lng,
      telephone
    });
  }

  //  下线,修改状态
  _updateStatus(id, status) {
    return this.where({
      id
    }).update({
      status,
      modify_time: think.datetime()
    });
  }
  // 修改审核状态
  _updateAuthStep(id, step) {
    return this.where({
      id
    }).update({
      auth_step: step,
      modify_time: think.datetime()
    });
  }
  // online 上线
  setOnline(id) {
    return this._updateStatus(id, STATUS.ONLINE);
  }
  paySuccess(id, paymentId) {
    return this.where({
      id,
      is_payed: PAY_STATUS.NO_PAYED
    }).update({
      pay_time: think.datetime(),
      status: STATUS.ONLINE,
      is_payed: PAY_STATUS.PAYED,
      payment_id: paymentId,
      modify_time: think.datetime()
    });
  }
  // offline 下线
  setOffline(id) {
    return this._updateStatus(id, STATUS.OFFLINE);
  }
  // 审核通过
  setPassed(id) {
    return this._updateAuthStep(id, AUTH_STEP.PASSED);
  }
  // 审核不通过
  setNoPassed(id) {
    return this._updateAuthStep(id, AUTH_STEP.NO_PASSED);
  }
  // 删除，逻辑删除
  logicDeleteById(id) {
    return this._updateStatus(id, STATUS.DELETED);
  }
  // 物理删除
  physicDeleteById(id) {
    return this.where({
      id
    }).delete();
  }
  // 恢复到上线
  recovery2OnlineById(id) {
    return this._updateStatus(id, STATUS.ONLINE);
  }
  // 恢复到下线
  recovery2OfflineById(id) {
    return this._updateStatus(id, STATUS.OFFLINE);
  }
  removeById(userId, id) {
    return this.where({
      id,
      user_id: userId
    }).delete();
  }
};
